package com.tank.entity;

import java.io.Serializable;
import java.util.concurrent.CopyOnWriteArrayList;

import com.tank.view.TankPanel;
import com.tank.view.thread.BulletRepaintThread;

public class Tank1 extends Element implements Serializable{

	private CopyOnWriteArrayList<Bullet> bullets = new CopyOnWriteArrayList<>();
	private Integer speed = 4;
	private Integer direct;
	private boolean isCrash; // 是否碰撞

	/**
	 * 坦克往北走
	 */
	public void goNorth() {
		this.setDirect(NORTH);
		if (this.getY() > 20) {
			this.setY(this.getY() - this.speed);
		}

	}

	/**
	 * 坦克往南走
	 */
	public void goSouth() {
		this.setDirect(SOUTH);
		if (this.getY() < TankPanel.HEIGHT - 20) {
			this.setY(this.getY() + this.speed);
		}
	}

	/**
	 * 坦克往西走
	 */
	public void goWest() {
		this.setDirect(WEST);
		if (this.getX() > 20 && this.getY() <= TankPanel.HEIGHT - 20) {
			this.setX(this.getX() - this.speed);
		}
	}

	/**
	 * 坦克往东走
	 */
	public void goEast() {
		this.setDirect(EAST);
		if (this.getX() < TankPanel.WIDTH - 20 && this.getY() <= TankPanel.HEIGHT - 20) {
			this.setX(this.getX() + this.speed);
		}
	}

	public Tank1(Integer x, Integer y, Integer direct) {
		super(x, y);
		bullets = new CopyOnWriteArrayList<Bullet>();
		this.direct = direct;
		this.setType(TANK);
		this.setWidth(40);
		this.setHeight(40);
	}

	/**
	 * 发射子弹
	 * 
	 * @param tank
	 */
	public void shot() {
		Bullet bullet = null;
		switch (this.getDirect()) { // 选择坦克的方向
		case NORTH:
			bullet = new Bullet(this.getX(), this.getY() - 20, NORTH);
			break;
		case SOUTH:
			bullet = new Bullet(this.getX(), this.getY() + 20, SOUTH);
			break;
		case WEST:
			bullet = new Bullet(this.getX() - 20, this.getY(), WEST);
			break;
		case EAST:
			bullet = new Bullet(this.getX() + 20, this.getY(), EAST);
			break;
		}
		this.getBullets().add(bullet);
		Thread t = new Thread(new BulletRepaintThread(bullet));
		t.start();
	}

	/**
	 * 判断坦克是否与另一个事物重叠
	 */
	public boolean Overlap(Element element) {
		int x = element.getX();
		int y = element.getY();
		if (this.getDirect() == Tank.NORTH) {
			this.setY(this.getY() - this.getSpeed()); // 先假设该坦克往前移动一步
			if (Math.abs(this.getY() - y) < element.getWidth() / 2 + this.getWidth() / 2
					&& Math.abs(this.getX() - x) < element.getWidth() / 2 + this.getWidth() / 2) {
				this.setY(this.getY() + this.getSpeed());
				return true;
			}
			this.setY(this.getY() + this.getSpeed());
		}
		if (this.getDirect() == Tank.SOUTH) {
			this.setY(this.getY() + this.getSpeed()); // 先假设该坦克往前移动一步
			if (Math.abs(this.getY() - y) < element.getWidth() / 2 + this.getWidth() / 2
					&& Math.abs(this.getX() - x) < element.getWidth() / 2 + this.getWidth() / 2) {
				this.setY(this.getY() - this.getSpeed());
				return true;
			}
			this.setY(this.getY() - this.getSpeed());
		}
		if (this.getDirect() == Tank.EAST) {
			this.setX(this.getX() + this.getSpeed());
			if (Math.abs(this.getY() - y) < element.getWidth() / 2 + this.getWidth() / 2
					&& Math.abs(this.getX() - x) < element.getWidth() / 2 + this.getWidth() / 2) {
				this.setX(this.getX() - this.getSpeed());
				return true;
			}
			this.setX(this.getX() - this.getSpeed());
		}
		if (this.getDirect() == Tank.WEST) {
			this.setX(this.getX() - this.getSpeed());
			if (Math.abs(this.getY() - y) < element.getWidth() / 2 + this.getWidth() / 2
					&& Math.abs(this.getX() - x) < element.getWidth() / 2 + this.getWidth() / 2) {
				this.setX(this.getX() + this.getSpeed());
				return true;
			}
			this.setX(this.getX() + this.getSpeed());
		}
		return false;
	}

	public CopyOnWriteArrayList<Bullet> getBullets() {
		return bullets;
	}

	public void setBullets(CopyOnWriteArrayList<Bullet> bullets) {
		this.bullets = bullets;
	}

	public Integer getSpeed() {
		return speed;
	}

	public void setSpeed(Integer speed) {
		this.speed = speed;
	}

	public int getDirect() {
		return direct;
	}

	public void setDirect(Integer direct) {
		this.direct = direct;
	}

	public boolean isCrash() {
		return isCrash;
	}

	public void setCrash(boolean isCrash) {
		this.isCrash = isCrash;
	}

}
